package com.editso.haier.result.topic;

import com.editso.haier.entity.Topic;
import com.editso.haier.result.commodity.DetailsSecondary;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

/**
 *  简要信息返回
 * @see Topic
 * @see com.editso.haier.dao.TopicDao
 * */
@Getter
@Setter
public class SimpleTopic {

    private Integer parentId;
    private Integer id;
    private String name;
    private Set<SimpleTopic> children = new HashSet<>();
    private Set<DetailsSecondary> secondaries = new HashSet<>();
    // 所有商品数量
    private Integer commTotal;
    private Date createDate;
    private String desc;


    public static SimpleTopic from(Topic topic, boolean lazy){
        return from(topic, lazy, false);
    }

    public static Set<SimpleTopic> from(Iterable<Topic> topics, boolean lazy){
        return from(topics, lazy, false);
    }

    public static Set<SimpleTopic> from(Iterable<Topic> topics, boolean lazy, boolean secondary){
        Set<SimpleTopic> cards = new HashSet<>();
        for (Topic topic : topics)
            cards.add(from(topic, lazy, secondary));
        return cards;
    }

    public static SimpleTopic from(Topic topic, boolean lazy, boolean secondary){
        if (topic == null) return null;
        SimpleTopic card = new SimpleTopic();
        card.setId(topic.getId());
        card.setName(topic.getName());
        card.setParentId(topic.getParentId());
        card.setCommTotal(topic.getCommodities().size());
        card.setCreateDate(topic.getCreateDate());
        card.setDesc(topic.getDescription());
        if (secondary) card.setSecondaries(DetailsSecondary.from(topic.getSecondaries()));
        if (lazy) return card;
        card.setChildren(from(topic.getChildren(), false, secondary) );
        return card;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SimpleTopic card = (SimpleTopic) o;
        return Objects.equals(id, card.id) && Objects.equals(name, card.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}
